package 杂项和预实验;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class myframe extends JFrame implements ActionListener {
    public String USER,PASSWORD;
    public JTextField user=new JTextField();
    public JTextArea password=new JTextArea();
    myframe(){
        setSize(800,600);
        setLayout(new BorderLayout(30,30));
        JPanel pan1=new JPanel();
        JPanel pan2=new JPanel();
        JPanel pan3=new JPanel();
        add(pan1,BorderLayout.NORTH);
        add(pan2,BorderLayout.CENTER);
        add(pan3,BorderLayout.SOUTH);
        setTitle("登录");
        JLabel welcomeWord=new JLabel();
        welcomeWord.setText("欢迎访问本站:)");
        pan1.add(welcomeWord);
        user.setName("用户名");
        password.setName("密码");
        pan2.add(user);
        pan2.add(password);
        pan2.setLayout(new GridLayout());
        JButton confirm=new JButton("登录");
        pan3.add(confirm);
        pan3.setLayout(new GridLayout());
        confirm.addActionListener(this);
        setVisible(true);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        USER=user.getText();
        PASSWORD=password.getText();
        if(e.getActionCommand().toString().equals("登录")){

            try {
            Class.forName("com.mysql.cj.jdbc.Driver");/*加载驱动程序*/
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
                            "student?useSSL=false&serverTimezone=UTC", "root","12345SSDLh");/*
                            创建连接对象，给明用户名密码，上方的student是表名称*/
            Statement stat=connection.createStatement();/*创建语句准备执行数据库命令*/
            String command="select StudentName,StudentID,StudentPassword from student.student";/*按照sql语法写
            出来的语句*/
            stat.execute(command);/*执行*/
            ResultSet seta=stat.executeQuery(command);/*这是结果集，把上方的命令的结果存起来的容器*/
            while (seta.next()){/*左边式子是一个boolen值判断是否还有下一行*/
                if (USER.equals(seta.getString(2)) && PASSWORD.equals(seta.getString(3))){
                    JDialog inform =new JDialog();
                    JLabel wel=new JLabel("登陆成功！欢迎"+seta.getString(1)+"同学！");
                    inform.add(wel);
                    inform.setSize(80,60);
                    inform.setVisible(true);
                }
                System.out.println(USER+"  "+PASSWORD);
            }
            seta.close();
            stat.close();
            connection.close();/*依次关闭实例化对象，断开连接*/
        } catch (ClassNotFoundException classNotFoundException) {
            classNotFoundException.printStackTrace();
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    }
}


